local skel = fk.CreateSkill {
  name = "rmt__zhizhuan",
  tags = { Skill.Switch },
}

Fk:loadTranslationTable{
  ["rmt__zhizhuan"] = "枝转",
  [":rmt__zhizhuan"] = "转换技，每回合限一次，准备阶段或结束阶段，你可以：阳：弃置一半手牌，然后摸手牌数的牌；阴：摸手牌数的牌，"..
  "然后弃置一半手牌。若以此法弃置牌时需取整，则跳过弃置流程，且本次不进行转换。",

  ["#rmt__zhizhuan"] = "枝转",
  ["#rmt__zhizhuan_yang-invoke"] = "枝转：是否弃置一半手牌（%arg张），然后摸手牌数的牌？",
  ["#rmt__zhizhuan_yin-invoke"] = "枝转：是否摸手牌数的牌，然后弃置一半手牌？",

  ["$rmt__zhizhuan1"] = "听离鸿之晨吟兮，望流火之余景。",
  ["$rmt__zhizhuan2"] = "感冬索而春敷兮，嗟夏茂而秋落。",
}

skel:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and player:usedSkillTimes(skel.name) == 0
    and (player.phase == Player.Start or player.phase == Player.Finish)
  end,
  on_cost = function(self, event, target, player, data)
    if player:getSwitchSkillState(skel.name, false) == fk.SwitchYang then
      if player:isKongcheng() or player:getHandcardNum() % 2 == 1 then
        return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#rmt__zhizhuan_yang-invoke:::0" })
      else
        local n = player:getHandcardNum() // 2
        local card = player.room:askToDiscard(player, {
        min_num = n,
        max_num = n,
        include_equip = false,
        skill_name = skel.name,
        cancelable = true,
        prompt = "#rmt__zhizhuan_yang-invoke:::"..n,
        skip = true,
        })
        if #card > 0 then
          event:setCostData(self, {cards = card})
          return true
        end
      end
    else
      return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#rmt__zhizhuan_yin-invoke" })
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:getSwitchSkillState(skel.name, true) == fk.SwitchYang then
      if player:getHandcardNum() % 2 == 1 then
        room:setPlayerMark(player, MarkEnum.SwithSkillPreName..skel.name, player:getSwitchSkillState(skel.name, true))
        player:setSkillUseHistory(skel.name, player:usedSkillTimes(skel.name, Player.HistoryGame) - 1, Player.HistoryGame)
        player:drawCards(player:getHandcardNum(), skel.name)
      else
        if not player:isKongcheng() then
          room:throwCard(event:getCostData(self).cards, skel.name, player, player)
          if not player.dead and not player:isKongcheng() then
            player:drawCards(player:getHandcardNum(), skel.name)
          end
        end
      end
    else
      if not player:isKongcheng() then
        player:drawCards(player:getHandcardNum(), skel.name)
      end
      if player:getHandcardNum() % 2 == 0 then
        if not player.dead and not player:isKongcheng() then
          local n = player:getHandcardNum() // 2
          room:askToDiscard(player, {
            max_num = n, min_num = n, include_equip = false, cancelable = false, skill_name = skel.name
          })
        end
      else
        room:setPlayerMark(player, MarkEnum.SwithSkillPreName..skel.name, player:getSwitchSkillState(skel.name, true))
        player:setSkillUseHistory(skel.name, player:usedSkillTimes(skel.name, Player.HistoryGame) - 1, Player.HistoryGame)
      end
    end
  end,
})

return skel
